package esdemo;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.junit.Before;
import org.junit.Test;
import static org.elasticsearch.index.query.QueryBuilders.*;

public class CountApi {
	private TransportClient client;

	@Before
	public void init() throws UnknownHostException {
		client = TransportClient.builder().build()
				.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
	}

	/**
	 * 计数
	 */
	@Test
	public void test01() {
		CountResponse response = client.prepareCount("twitter")
		        .setQuery(termQuery("user", "kimchy2"))
		        .execute()
		        .actionGet();
		System.out.println(response.getCount());
	}
}
